package com.getpebble.android.bluetooth.radio;

import android.annotation.SuppressLint;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothManager;
import android.content.Context;
import android.os.Build.VERSION;
import com.getpebble.android.common.core.trace.Trace;
import java.lang.ref.WeakReference;
import java.util.HashSet;
import java.util.Set;

public class PebbleBluetoothAdapter {
    private static final String TAG = PebbleBluetoothAdapter.class.getSimpleName();
    private static BluetoothAdapter sBluetoothAdapter = null;
    private static PebbleBluetoothAdapter sInstance = null;
    private WeakReference<Context> mContextWeakReference = null;

    public enum AdapterState {
        TURNING_OFF,
        OFF,
        TURNING_ON,
        ON,
        ERROR
    }

    private PebbleBluetoothAdapter(Context context) {
        this.mContextWeakReference = new WeakReference(context);
        sBluetoothAdapter = getDefaultBluetoothAdapter();
    }

    public static PebbleBluetoothAdapter createInstance(Context context) throws IllegalArgumentException {
        if (context == null) {
            throw new IllegalArgumentException("'context' cannot be null!");
        }
        if (sInstance == null) {
            sInstance = new PebbleBluetoothAdapter(context);
        }
        return sInstance;
    }

    @SuppressLint({"NewApi"})
    private BluetoothAdapter getDefaultBluetoothAdapter() {
        Context context = (Context) this.mContextWeakReference.get();
        if (VERSION.SDK_INT < 18 || context == null) {
            return BluetoothAdapter.getDefaultAdapter();
        }
        return ((BluetoothManager) context.getSystemService("bluetooth")).getAdapter();
    }

    public boolean cancelDiscovery() {
        Trace.debug(TAG, "cancelDiscovery()");
        return hasDevice() && sBluetoothAdapter.cancelDiscovery();
    }

    public boolean isDiscovering() {
        Trace.debug(TAG, "isDiscovering()");
        return hasDevice() && sBluetoothAdapter.isDiscovering();
    }

    public PebbleBluetoothDevice getPebbleBluetoothDevice(String address) {
        if (hasDevice()) {
            return new PebbleBluetoothDevice(sBluetoothAdapter.getRemoteDevice(address));
        }
        return null;
    }

    public boolean isEnabled() {
        return hasDevice() && sBluetoothAdapter.isEnabled();
    }

    public boolean startDiscovery() {
        Trace.debug(TAG, "startDiscovery() hasDevice = " + hasDevice());
        return hasDevice() && sBluetoothAdapter.startDiscovery();
    }

    public boolean hasDevice() {
        return sBluetoothAdapter != null;
    }

    public Set<PebbleBluetoothDevice> getBondedDevices() {
        Set<PebbleBluetoothDevice> devices = new HashSet();
        if (hasDevice()) {
            for (BluetoothDevice androidDevice : sBluetoothAdapter.getBondedDevices()) {
                devices.add(new PebbleBluetoothDevice(androidDevice));
            }
        }
        Trace.debug(TAG, "getBondedDevices() Returning: " + devices.size());
        return devices;
    }
}
